<?php
namespace app\api\controller;

use app\common\model\Article as ArticleModel;
use http\Env\Request;
use think\Config;
class Article extends Base
{
    protected $noNeedLogin = ['*'];
    //获取专家 分享  推荐
    public function getList2()
    {
        $articleModel =new  ArticleModel();
        //推荐1 关注2  专家分享3
        $type = $this->request->get('type',1);
        $where = [];
        if($type ==1){
            $where['type']= 2;
            $where['user_id']=['neq',$this->auth->id];
        }elseif($type==2){
            
        }else{
            //专家
            $where['type']= 1;
        }
        $art_list  = $articleModel->getList($where,false);
        $this->success('请求成功',$art_list,200);
    }

    //专家 关注 推荐
    public function getList()
    {
        $articleModel =new  ArticleModel();
        //推荐1 关注2  专家分享3
        $type = $this->request->get('type',1);
        $is_page = $this->request->get('is_page')?true:false;
        $where = [];
        $follow=false;
        $uid =$this->auth->id?$this->auth->id:null;
        if($type ==1){
            $where['type']= 2;
//            if($uid){
//                $where['user_id']=['neq',$this->auth->id];
//            }

        }elseif($type==2){
            if(!$uid){
                $this->success('请求成功',[],200);
            }
            //关注的 加上评论
            $follow_model = new Follow();
            $info = $follow_model->myFollowList(1);
            $info[]=$uid;
            if($info){
                $where['user_id'] = ['in',$info];
            }
            $follow = true;
        }else{
            //专家
            $where['type']= 1;
        }
        $art_list  = $articleModel->getList($where,$is_page,$follow,$uid);
        $this->success('请求成功',$art_list,200);
    }
    
    //文章详情
    public function detail()
    {
        $id = $this->request->get('id');
        if(!$id)
        {
            $this->error('参数不正确');
        }
        $articleModel =new  \app\common\model\Article();
        $articleModel->where('id',$id)->setInc('views');
        $article = $articleModel
            ->alias('a')
            ->field('a.*,u.nickname,u.avatar')
            ->join('user u','a.user_id = u.id','left')
            ->where('a.id',$id)
            ->find();
        if(!$article){
            $this->error('文章未找到');
        }
//        $where['aid']= $id;
//        $commentList = $this->comment($where);
//        $commentList = $this->getOne($id);
//        if($commentList){
//            //判断是否是作者
//            $commentList = $this->is_author($commentList,$article['user_id']);
//        }
        if($article['images']){
            $images = explode(',',$article['images']);
            foreach ($images as &$v){
                $v=request()->domain().$v;
            }
            $article['images'] = $images;
        }
        if($article['video']){
            $article['video'] = request()->domain().$article['video'];
        }
        if($article['cover']){
            $article['cover'] = request()->domain().$article['cover'];
        }
        $article['createtime']=$article['createtime']?date('Y-m-d H:i',$article['createtime']):'';
        if(!$article['user_id'] && $article['type'] == 1){
            $article['nickname'] = Config::get('site.nickname');
            $article['avatar'] = cdnurl(Config::get('site.avatar'),true);
        }
        $article['is_likes'] = 0;
        $article['is_collects'] = 0;
        $article['is_follow'] = 0;
        if($this->auth->id){
            //查看文章是否被本人点赞过
            $vote=new \app\common\model\Vote();
            $is_vote = $vote->read(['aid'=>$id,'uid'=>$this->auth->id]);
            if($is_vote){
                $article['is_likes'] =1;
            }
            $Collection=new \app\common\model\Collection();
            $is_Collection = $Collection->read(['aid'=>$id,'uid'=>$this->auth->id]);
            if($is_Collection){
                $article['is_collects'] =1;
            }

            $info=[];
            $follow_model = new Follow();
            $info = $follow_model->myFollowList(1);
            if(in_array($article['user_id'],$info)){
                $article['is_follow'] = 1;
            }
        }
        $this->success('获取成功',$article,200);
    }

    public function comment($where)
    {
        $commentModel = new \app\common\model\Comment();
        $list  = $commentModel->getList($where);
        $temp = [];
        foreach ($list as $key => &$comment) {
            //parent_id为0的调用getSubtree方法进行递归循环
            if ($comment['pid'] == 0) {
                unset($list[$key]);
                $comment['childrenList'] = $this->setChild($comment['child']);
                //按照时间顺序排序
                unset($comment['child']);
                $temp[] = $comment;
            }
        }
        return $temp;
    }

    //递归减少子节点
    public function setChild($list){
        $temp=[];
        $temp_list=[];
        //获取到子节点
        if(isset($list)){
            foreach ($list as $key=>&$comment){
                unset($list[$key]);
                $old=$comment;
                unset($comment['child']);
                $temp[]= $comment;
                if(isset($old['child'])){
                    $temp_list = $this->setChild($old['child']);
                    unset($old['child']);

                }else{
                    unset($old['child']);
                }
                unset($old);
                $temp=array_merge($temp,$temp_list);
            }
        }
        return $temp;
    }

    //判断是不是作者
    public function is_author_old($data=[],$author_id)
    {
        //判断评论人是不是作者
        foreach ($data as &$v){
            //顶级的是否是作者
            $v['user_author'] = 0;
            $v['to_user_author'] = 0;
            if($v['to_user_id'] == $author_id){
                $v['user_author'] = 1;
            }
            if($v['to_user_author'] == $author_id){
                $v['user_author'] = 1;
            }
            if($v['childrenList']){
                foreach ($v['childrenList'] as &$vv){
                    $vv['user_author'] = 0;
                    $vv['to_user_author'] = 0;
                    if($vv['to_user_id'] == $author_id){
                        $vv['user_author'] = 1;
                    }
                    if($vv['to_user_author'] == $author_id){
                        $vv['user_author'] = 1;
                    }
                }
            }
        }
        return $data;
    }


    //获取到所有的对文章的评论 $id,
    public function getOne()
    {
        $id =$this->request->param('id');//文章的id
        $limit =$this->request->param('limit',10);//限制
        $where['c.aid']=$id;
        $commentModel =new \app\common\model\Comment();
        $articleModel =new  \app\common\model\Article();
        $article = $articleModel::get($id);
        if(!$article){
            $this->error('文章不存在');
        }
        $info =$commentModel->getAidList($where,true);
        $info =json_decode(json_encode($info),true);
        $commentList=[];
        if(isset($info['data']) && $info['data']){
            //判断是否是作者
            $commentList = $this->is_author($info['data'],$article['user_id']);
        }
        $info['data']=$commentList;
        $this->success('获取成功',$info);
    }

    //判断是不是作者
    public function is_author($data=[],$author_id)
    {
        //判断评论人是不是作者
        foreach ($data as &$v){
            //顶级的是否是作者
            $v['user_author'] = 0;
            if($v['user_id'] == $author_id){
                $v['user_author'] = 1;
            }
            //二级评论回复
            if(isset($v['reply']) && $v['reply']){
                foreach ($v['reply'] as &$vv){
                    $vv['user_author'] = 0;
                    $vv['to_user_author'] = 0;
                    if($vv['from_uid'] == $author_id){
                        $vv['user_author'] = 1;
                    }
                    if($vv['to_uid'] == $author_id){
                        $vv['to_user_author'] = 1;
                    }
                }
            }
        }
        return $data;
    }
}